--
-- @Author:      name
-- @DateTime:    2018-03-30 23:05:48
-- @Description: 节点内消息的处理

local skynet = require "skynet"
local socket = require "skynet.socket"
local httpc = require "http.httpc"

local MessageHandler = class("MessageHandler")

---------------------------------------------------------
-- Private
---------------------------------------------------------
function MessageHandler:ctor(message_dispatch, node_message)

	self.server_id = tonumber(skynet.getenv("server_id")) --节点名	
	self.message_dispatch = message_dispatch	
	self.node_message = node_message
	-- self.proto_loader = ProtoLoader.new() --proto加载
	self.debug_port = tonumber("29"..self.server_id)


	self:register()
	
end

--注册本服务里的消息
function MessageHandler:register()

	self.message_dispatch:registerSelf('start',handler(self, self.start))
	print("___________he")
end

---------------------------------------------------------
-- CMD
---------------------------------------------------------
function MessageHandler:start()


	--代理服务
	local agent = {}
	local agent_num = 1
	for i= 1, agent_num do
		agent[i] = skynet.newservice("agent_service", i)
	end
	local balance = 1
	local id = socket.listen("0.0.0.0", 8001)
	skynet.error("Listen web port 8001")
	local function start_callback(id, addr)
		-- skynet.error(string.format("%s connected, pass it to agent :%08x", addr, agent[balance]))
		-- print("____id__", id)
		skynet.send(agent[balance], "lua", "start", id)
		balance = balance + 1
		if balance > #agent then
			balance = 1
		end
	end	
	print("____socket.start__", id)
	socket.start(id, start_callback)

	-- skynet.sleep(200)
	-- print("________http_1111111")
	-- local host = "127.0.0.1:8001"
	-- local status, body = httpc.get(host, "/getGateReq")	
	-- print("________http__", status, body)

end






return MessageHandler